﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Hosting;
using UDS.Entity;

namespace UDS.Components
{
    /// <summary>
    /// 工作流的函数
    /// </summary>
    public static class DocumentFlow
    {
        /// <summary>
        /// 获得环节成员
        /// </summary>
        /// <param name="FlowID">流程ID</param>
        /// <param name="StepID">步骤ID</param>
        public static IList<StaffInfo> GetStaffInStep(int FlowID, int StepID)
        {
            var parameters = new[]
            {
                Database.MakeInParam("@FlowID", SqlDbType.Int, 4, FlowID),
                Database.MakeInParam("@StepID", SqlDbType.Int, 4, StepID)
            };

            return Staff.GetStaffList("dbo.sproc_Flow_GetStaffInStep_V2", parameters);
        }

        /// <summary>
        /// 获得环节成员
        /// </summary>
        /// <param name="DocID">文档ID</param>
        public static IList<StaffInfo> GetStaffInStep(int DocID)
        {
            var parameters = new[]
            {
                Database.MakeInParam("@DocID", SqlDbType.Int, 4, DocID)
            };

            return Staff.GetStaffList("dbo.sproc_Flow_GetStaffInStep_Ex_V2", parameters);
        }

        /// <summary>
        /// 获得步骤接受人员
        /// </summary>
        /// <param name="DocID">文档ID</param>
        public static IList<StaffInfo> GetReceiver(int DocID)
        {
            var parameters = new[]
            {
                Database.MakeInParam("@DocID", SqlDbType.Int, 4, DocID)
            };

            return Staff.GetStaffList("dbo.sproc_Flow_GetReceiver_V2", parameters);
        }

        /// <summary>
        /// 获得步骤已经签收人员
        /// </summary>
        /// <param name="DocID">文档ID</param>
        public static IList<StaffInfo> GetSignedList(int DocID)
        {
            var parameters = new[]
            {
                Database.MakeInParam("@DocID", SqlDbType.Int, 4, DocID)
            };

            return Staff.GetStaffList("dbo.sproc_Flow_GetSignedList_V2", parameters);
        }

        /// <summary>
        /// 获得步骤未签收人员
        /// </summary>
        /// <param name="DocID">文档ID</param>
        public static IList<StaffInfo> GetUnsignedList(int DocID)
        {
            var parameters = new[]
            {
                Database.MakeInParam("@DocID", SqlDbType.Int, 4, DocID)
            };

            return Staff.GetStaffList("dbo.sproc_Flow_GetUnsignedList_V2", parameters);
        }

        /// <summary>
        /// 获得步骤已经批阅人员
        /// </summary>
        /// <param name="DocID">文档ID</param>
        public static IList<StaffInfo> GetPostiler(int DocID)
        {
            var parameters = new[]
            {
                Database.MakeInParam("@DocID", SqlDbType.Int, 4, DocID)
            };

            return Staff.GetStaffList("dbo.sproc_Flow_GetPostiler_V2", parameters);
        }

        internal static void DeleteLocalAttachFile(string fileVirtualPath)
        {
            DeleteLocalAttachFile(fileVirtualPath, true);
        }

        internal static void DeleteLocalAttachFile(string fileVirtualPath, bool removeParentDirectory)
        {
            if (!string.IsNullOrEmpty(fileVirtualPath))
            {
                try
                {
                    HttpUtil.DeleteAttachFile(fileVirtualPath, removeParentDirectory);
                }
                catch (Exception exp)
                {
                    throw new InvalidOperationException(string.Format("Failed to delete old attachment: {0}", fileVirtualPath), exp);
                }
            }
        }

        /// <summary>
        /// 删除文档
        /// </summary>
        /// <param name="id">被删除的文档ID</param>
        public static int DeleteDocument(int id)
        {
            using (var db = new Database())
            {
                db.BeginTransaction();

                var parameters = new[]
                {
                    Database.MakeInParam("@DocID", SqlDbType.Int, 4, id)
                };

                // Get the path to the attached file.
                // TODO: what if a document has many attachments?
                string filePath = DocumentFile.GetAttachmentPath(id, db);

                int r = db.RunProc("dbo.sproc_Flow_DeleteDocument_V2", parameters);
                if (r != 0)
                {
                    throw new InvalidOperationException(string.Format("Failed to delete the specified document: {0}.", id));
                }

                // Remove the file stored on the local storage.
                DeleteLocalAttachFile(filePath);

                // Commit the transaction here
                db.Commit();
            }

            return 0;
        }
    }
}
